using UnityEngine;
using System.Collections;

public class Tile : MonoBehaviour
{
    #region Attributs
    public Vector2 position2D;
    public Const.TYPE_TILE type;
    public Texture[] textures;

    public bool editor;
    public bool IsHighLighted;
    public int indexTexture = 0;
    #endregion

    // Use this for initialization
	void Awake () {
        SetType(Const.TYPE_TILE.GROUND);

    }

    void Start()
    {
        position2D = new Vector2(this.transform.position.x, this.transform.position.z);
    }

    void OnMouseDown()
    {
#if UNITY_ANDROID
        //return;
#endif

        if(!editor)
            return;

        LevelEditorManager lvlEditor = (GameObject.FindObjectOfType(typeof(LevelEditorManager)) as LevelEditorManager);

        if (lvlEditor == null || lvlEditor.stateEditor == LevelEditorManager.STATE_EDITOR.ENEMYS)
            return;

        lvlEditor.currentTileSelected = this;
        

        //Rotate
        if (Input.GetKey(KeyCode.LeftShift))
        {
            this.transform.Rotate(Vector3.up, 90);
            return;
        }

        if (Input.GetKey(KeyCode.LeftControl))
        {
            //Change texture
            SetIndexTexture(indexTexture + 1);
            return;
        }

        if (Input.GetKey(KeyCode.W))
        {
            SetType(Const.TYPE_TILE.GROUND);
            return;
        }

        
        //if (type == Const.TYPE_TILE.WALL)
            //SetType(Const.TYPE_TILE.GROUND);
        //else
            //SetType(Const.TYPE_TILE.WALL);
        SetType((Const.TYPE_TILE)((int)type + 1));
        
    }

    public void SetType(Const.TYPE_TILE t)
    {
        indexTexture = 0;
        if(t == Const.TYPE_TILE.NB_TILE)
        {
            t = (Const.TYPE_TILE)0;
        }

        type = t;

        if (type != Const.TYPE_TILE.WALL)
        {
            if (IsHighLighted)
            {
                this.renderer.material.color = Color.red;
            }
            else
            {
                this.renderer.material.color = Color.white;
            }
        }

        this.renderer.material.SetTexture("_MainTex", textures[(int)type]);
    }

    public void SetIndexTexture(int index)
    {
        int nbTiles = GetNbTextureByType(type);
        if (index >= nbTiles)
            index = 0;

        this.renderer.material.mainTextureScale = new Vector2((1 / (float)nbTiles), 1);
        this.renderer.material.mainTextureOffset = new Vector2(index * (1 / (float)nbTiles), 0);
        
        indexTexture = index;
    }

    int GetNbTextureByType(Const.TYPE_TILE t)
    {
        switch (t)
        {
            case Const.TYPE_TILE.GROUND:
                return Const.nbGrounds;
            case Const.TYPE_TILE.WALL :
                return Const.nbWalls;
            case Const.TYPE_TILE.PILE_OS:
                return 1;
                break;
            case Const.TYPE_TILE.CAISSE:
                return 1;
                break;
            case Const.TYPE_TILE.END:
                return 1;
                break;
        }
        return 1;
    }
}
